<template>
  <el-dialog :title="title" v-model="dialogVisible" @close="dialogFormVisible" :close-on-click-modal="false"
    :close-on-press-escape="false">
    <el-form size="" ref="formRef" :model="form" label-width="200px" v-if="open_type != 'psw'">
      <el-form-item label="昵称" prop="nickName">
        <el-input class="max-w460" v-model="form.nickName" placeholder="请输入昵称"></el-input>
      </el-form-item>
      <el-form-item label="姓名" prop="real_name">
        <el-input class="max-w460" v-model="form.real_name" placeholder="请输入真实姓名"></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input class="max-w460" v-model="form.mobile" placeholder="请输入手机号"></el-input>
      </el-form-item>
      <el-form-item label="头像">
        <el-row>
          <el-button icon="Picture" @click="openUpload('avatarUrl')">选择图片</el-button>
          <div v-if="form.avatarUrl != ''" class="img">
            <img :src="form.avatarUrl" width="100" height="100" @click="openUpload('avatarUrl')" />
          </div>
        </el-row>
      </el-form-item>
      <el-form-item label="性别">
        <el-radio-group v-model="form.gender">
          <el-radio :value="1">男</el-radio>
          <el-radio :value="0">女</el-radio>
          <el-radio :value="2">保密</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="密码" prop="password" v-if="open_type === 'add'">
        <el-input class="max-w460" type="password" show-password v-model="form.password" placeholder="请输入密码"></el-input>
      </el-form-item>
      <el-form-item label="地区">
        <el-cascader v-model="form.region" :options="regions" :props="{ label: 'name', value: 'id' }"
          @change="userRegion" />
      </el-form-item>
      <el-form-item label="厂家">
        <el-select v-model="form.carfilm_factory" placeholder="请选择" clearable class="max-w460">
          <el-option v-for="(item, index) in factories" :key="index" :label="item.factory_name"
            :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="Boss账号">
        <el-radio-group v-model="form.factory_boss">
          <el-radio :value="1">是</el-radio>
          <el-radio :value="0">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="财务" v-auth="'/carfilm/factory/user'">
        <el-radio-group v-model="form.is_finance">
          <el-radio :value="1">是</el-radio>
          <el-radio :value="0">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="区域管理" v-auth="'/carfilm/factory/user'">
        <el-radio-group v-model="form.region_manager">
          <el-radio :value="1">是</el-radio>
          <el-radio :value="0">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="管理区域" v-if="form.region_manager">
        <el-cascader v-model="form.manager_region_val" :options="regions"
          :props="{ label: 'name', value: 'id', checkStrictly: true }" clearable @change="changeRegion" />
      </el-form-item>
    </el-form>
    <el-form size="" ref="formRef" :model="psw_form" label-width="200px" v-else>
      <el-form-item label="密码" prop="password">
        <el-input class="max-w460" type="password" v-model="psw_form.password" placeholder="请输入密码"></el-input>
      </el-form-item>
    </el-form>
    <!--上传图片组件-->
    <Upload v-if="isupload" :isupload="isupload" :type="type" @returnImgs="returnImgsFunc">上传图片</Upload>
    <template #footer>
      <el-button @click="cancelFunc">取消</el-button>
      <el-button type="primary" @click="onSubmit" :loading="loading">确定</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { adduser, editPassword, edituser, getuser } from "@/api/user.js";
import Upload from "@/components/file/Upload.vue";
import { formatModel } from "@/utils/base.js";
import { ElMessage } from "element-plus";
import { inject, onMounted, ref } from "vue";
const title = ref("编辑会员");
const dialogVisible = ref(false);
const factories = inject("factories") as any;
const form = ref({
  user_id: 0,
  nickName: "",
  real_name: "",
  avatarUrl: "",
  gender: 2,
  mobile: "",
  password: "",
  carfilm_factory: 0,
  factory_boss: 0,
  is_finance: 0,
  region_manager: 0,
  province: "",
  city: "",
  region: "",
  manager_province: "",
  manager_city: "",
  manager_region: "",
  manager_region_val: ""
});
const psw_form = ref({
  user_id: 0,
  password: "",
});
const formRef = ref<any>(null);
const loading = ref(false);
/*是否上传图片*/
const isupload = ref(false);
const regions = inject("regions") as any;
const dialogFormVisible = () => {
  dialogVisible.value = false;
};
const open_type = ref("");
const open = (type: string, id: number = 0) => {
  open_type.value = type;
  if (type == "edit") {
    title.value = "编辑会员";
    getuser({ user_id: id }).then((data) => {
      let model = data.data.model;
      form.value = formatModel(form.value, model);
      form.value.manager_region_val = form.value.manager_region ? form.value.manager_region : (
        form.value.manager_city ? form.value.manager_city : form.value.manager_province
      )
      console.log(form.value.manager_region_val)
      dialogVisible.value = true;
    });
  } else if (type == "add") {
    title.value = "添加会员";
    dialogVisible.value = true;
  } else if (type == "psw") {
    title.value = "修改密码";
    psw_form.value = {
      user_id: id,
      password: "",
    };
    dialogVisible.value = true;
  }
};
const emits = defineEmits(["doneSave"]);
const onSubmit = () => {
  formRef.value.validate((valid: any) => {
    if (valid) {
      loading.value = true;
      if (open_type.value == "add") {
        adduser(form.value)
          .then((data: any) => {
            ElMessage({
              message: "添加成功",
              type: "success",
            });
            loading.value = false;
            dialogFormVisible();
            emits("doneSave");
          })
          .catch((error) => {
            loading.value = false;
          });
      } else if (open_type.value == "edit") {
        edituser(form.value)
          .then((data: any) => {
            ElMessage({
              message: "恭喜你，修改成功",
              type: "success",
            });
            loading.value = false;
            dialogFormVisible();
            emits("doneSave");
          })
          .catch((error) => {
            loading.value = false;
          });
      } else {
        editPassword(psw_form.value)
          .then((data: any) => {
            ElMessage({
              message: "修改成功",
              type: "success",
            });
            loading.value = false;
            dialogFormVisible();
          })
          .catch((error) => {
            loading.value = false;
          });
      }
    }
  });
};
const type = ref("");
/*上传*/
const openUpload = (e: any) => {
  type.value = e;
  isupload.value = true;
};
/*获取图片*/
const returnImgsFunc = (e: any) => {
  if (e != null && e.length > 0) {
    if (type.value == "avatarUrl") {
      form.value.avatarUrl = e[0].file_path;
    }
  }
  isupload.value = false;
};
const userRegion = (e: any) => {
  form.value.province = e[0];
  form.value.city = e[1] ?? "";
  form.value.region = e[2] ?? "";

}
const changeRegion = (e: any) => {
  console.log(e);
  if (e != null && e.length > 0) {
    form.value.manager_province = e[0];
    form.value.manager_city = e[1] ?? "";
    form.value.manager_region = e[2] ?? "";
    form.value.manager_region_val = e[2] ?? (e[1] ?? e[0]);
  }
};
/*取消*/
const cancelFunc = () => {
  dialogFormVisible();
};
defineExpose({ open });
</script>

<style></style>
